﻿using System;
using System.Collections.Generic;
using StockholmsStad.Web.UI.Class.Interfaces;
using StockholmsStad.Web.UI.UIServiceReference;

namespace StockholmsStad.Web.UI.Class.Facade
{
    internal class WebServiceFacade : IServiceFacade
    {
        #region IServiceFacade Members

        private readonly static User ANONYMOUS_USER = new User { Role = UserRole.ReadOnly };

        /// <summary>
        /// Alla tillgängliga ceremonispråk
        /// </summary>
        public ValueIdPair[] CeremonyLanguages
        {
            get
            {
                var client = new WebServiceClient();

                try
                {
                    client.Open();
                    var list = client.GetCeremonyLanguages(ANONYMOUS_USER);
                    client.Close();
                    return list;
                }
                catch (Exception)
                {
                    client.Abort();
                    throw;
                }
            }
        }

        /// <summary>
        /// Alla tillgängliga specialbehov
        /// </summary>
        public ValueIdPair[] CeremonySpecialNeeds
        {
            get
            {
                var client = new WebServiceClient();

                try
                {
                    client.Open();
                    var list = client.GetCeremonySpecialNeeds(ANONYMOUS_USER);
                    client.Close();
                    return list;
                }
                catch (Exception)
                {
                    client.Abort();
                    throw;
                }
            }
        }

        /// <summary>
        /// Alla tillgängliga vigselbevisspråk
        /// </summary>
        public ValueIdPair[] WeddingCertificateLanguages
        {
            get
            {
                var client = new WebServiceClient();

                try
                {
                    client.Open();
                    var list = client.GetWeddingCertificateLanguages(ANONYMOUS_USER);
                    client.Close();
                    return list;
                }
                catch (Exception)
                {
                    client.Abort();
                    throw;
                }
            }
        }

        /// <summary>
        /// Alla lediga tider som går att boka för en vigsel
        /// </summary>
        public IList<TimeBlock> GetFreeTimeblocks(User user, bool ordinary)
        {
            var client = new WebServiceClient();

            try
            {
                client.Open();
                var list = client.GetFreeTimeBlockDateTimes(user, ordinary);
                client.Close();
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        /// <summary>
        /// Alla bokade dagar som går att sätta som önskat datum
        /// </summary>
        public IList<TimeBlock> GetBookedTimeblocks(User user)
        {
            var client = new WebServiceClient();

            try
            {
                client.Open();
                var list = client.GetBookedTimeBlockDates(user);
                client.Close();
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        ///<summary>
        /// Skickar ett mail till användaren med det befintliga lösenordet
        ///</summary>
        ///<param name="email"></param>
        public void SendForgottenPassword(string email)
        {
            var client = new WebServiceClient();

            try
            {
                client.Open();
                client.SendForgottenPassword(email);
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        ///<summary>
        /// Hämtar alla historikloggar för användaren.
        ///</summary>
        ///<param name="user"></param>
        ///<returns></returns>
        public History[] GetHistoryForCurrentUser(User user)
        {
            var client = new WebServiceClient();

            try
            {
                client.Open();
                var result = client.GetHistoryForCurrentUser(user);
                client.Close();
                return result;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        /// <summary>
        /// Alla tillgängliga ceremonityper. Borde vara Stadshuset och Individuell
        /// </summary>
        public IList<ValueIdPair> CeremonyTypes
        {
            get
            {
                var client = new WebServiceClient();

                try
                {
                    client.Open();
                    var list = client.GetCeremonyTypes(ANONYMOUS_USER);
                    client.Close();
                    return list;
                }
                catch (Exception)
                {
                    client.Abort();
                    throw;
                }
            }
        }

        /// <summary>
        /// Registrera par på servern.
        /// </summary>
        /// <param name="couple"></param>
        /// <param name="ceremony"></param>
        /// <returns>ticketId</returns>
        public int RegisterCouple(Couple couple, Ceremony ceremony)
        {
            var client = new WebServiceClient();

            try
            {
                client.Open();
                var id = client.RegisterCouple(couple, ceremony);
                client.Close();
                return id;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        /// <summary>
        /// Uppdaterar redan registrerat par.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="couple"></param>
        /// <param name="ceremony"></param>
        /// <param name="ticketId"></param>
        public void UpdateCouple(User user, Couple couple, Ceremony ceremony, int ticketId)
        {
            var client = new WebServiceClient();

            try
            {
                System.Threading.Thread.Sleep(6000);

                client.Open();
                client.UpdateCouple(user, couple, ceremony, ticketId);
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        /// <summary>
        /// Returnerar redan existerande bokning
        /// </summary>
        /// <param name="user"></param>
        /// <param name="ticketId"></param>
        /// <returns></returns>
        public Booking GetBooking(User user, int ticketId)
        {
            var client = new WebServiceClient();

            try
            {
                client.Open();
                var booking = client.GetBooking(user, ticketId);
                client.Close();
                return booking;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        /// <summary>
        /// Returnerar redan existerande par
        /// </summary>
        /// <param name="user"></param>
        /// <param name="ticketId"></param>
        /// <returns></returns>
        public Couple GetCouple(User user, int ticketId)
        {
            var client = new WebServiceClient();

            try
            {
                client.Open();
                var couple = client.GetTicket(user, ticketId).Couple;
                client.Close();
                return couple;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int GetTicketId(User user)
        {
            var client = new WebServiceClient();

            try
            {
                client.Open();
                var id = client.GetTicketId(user);
                client.Close();
                return id;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        /// <summary>
        /// Returnerar redan existerande ceremoni
        /// </summary>
        /// <param name="user"></param>
        /// <param name="ticketId"></param>
        /// <returns></returns>
        public Ceremony GetCeremony(User user, int ticketId)
        {
            var client = new WebServiceClient();

            try
            {
                client.Open();
                Ceremony ceremony = client.GetTicket(user, ticketId).Ceremony;
                client.Close();
                if (ceremony.SpecialNeeds == null)
                    ceremony.SpecialNeeds = new ValueIdPair[0];
                return ceremony;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        /// <summary>
        /// Lägger till eller uppdaterar en bokning
        /// </summary>
        /// <param name="booking"></param>
        /// <param name="user"></param>
        /// <param name="ticketId"></param>
        public void RegisterBooking(User user, Booking booking, int ticketId)
        {
            var client = new WebServiceClient();

            try
            {
                client.Open();
                client.SaveBooking(user, booking, ticketId);
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        /// <param name="booking"></param>
        /// <param name="ticketId"></param>
        public void CancelBooking(User user, Booking booking, int ticketId)
        {
            if (booking == null)
            {
                return;
            }

            var client = new WebServiceClient();

            try
            {
                client.Open();
                client.CancelBooking(user, ticketId, booking);
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        ///<summary>
        /// Kontrollera att username och password tillhör en giltig användare
        ///</summary>
        ///<param name="username"></param>
        ///<param name="password"></param>
        ///<returns></returns>
        public bool Login(string username, string password)
        {
            var client = new WebServiceClient();

            try
            {
                client.Open();
                var validUser = client.IsValidWebUser(username, password);
                client.Close();
                return validUser;

            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        /// <summary>
        /// Returnerar användaren med specificerat användarnamn
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public User GetUser(string username)
        {
            var client = new WebServiceClient();

            try
            {
                client.Open();
                var user = client.GetUser(username);
                client.Close();
                return user;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        /// <summary>
        /// Returnerar true om bokningskoden matchas av en förrättare
        /// </summary>
        /// <param name="text"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool IsBookingCodeValid(User user, string text)
        {
            var client = new WebServiceClient();

            try
            {
                client.Open();
                var isValid = client.IsBookingCodeValid(user, text);
                client.Close();
                return isValid;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        ///<summary>
        /// Tar reda på om användarnamnet är unikt
        ///</summary>
        ///<param name="user"></param>
        ///<param name="username"></param>
        ///<returns></returns>
        public bool IsUserNameUnique(User user, string username)
        {
            var client = new WebServiceClient();

            try
            {
                client.Open();
                var isUnique = client.IsUserNameUnique(user, username);
                client.Close();
                return isUnique;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        /// <summary>
        /// Returnerar true om bokningen kan uppdateras från webbapplikationen
        /// </summary>
        /// <param name="booking"></param>
        /// <returns></returns>
        public bool IsUpdateable(Booking booking)
        {
            var client = new WebServiceClient();

            try
            {
                client.Open();
                var isUpdatable = client.IsUpdateable(booking);
                client.Close();
                return isUpdatable;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public IList<InformationText> GetTexts(User user, int textTypeId)
        {
            var client = new WebServiceClient();

            try
            {
                client.Open();
                var list = client.GetTexts(user, textTypeId);
                client.Close();
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public string GetDatabaseVersion()
        {
            var client = new WebServiceClient();

            try
            {
                client.Open();
                var version = client.GetDatabaseVersion(ANONYMOUS_USER);
                client.Close();
                return version;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public string GetServiceVersion()
        {
            var client = new WebServiceClient();

            try
            {
                client.Open();
                var version = client.GetServiceVersion(ANONYMOUS_USER);
                client.Close();
                return version;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        #endregion
    }
}